package com.woniu.service.impl;

import com.woniu.entity.Category;
import com.woniu.entity.Product;
import com.woniu.mapper.CategoryMapper;
import com.woniu.mapper.ProductMapper;
import com.woniu.mapper.ProductMapper;
import com.woniu.service.CategoryService;
import com.woniu.util.ConstantUtil;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ProductMapper productMapper;
    @Override
    public ResponseUtil findAll() {
        List<Category> all = categoryMapper.findAll();
        return ResponseUtil.get(ResponseEnum.OK, all);
    }
    public ResponseUtil findAll2() {
        List<Category> mainList = categoryMapper.findAll();
            //找主类别
        List<Category> parentList = mainList.stream()
                    .filter(e -> e.getTypeId().equals(0))
                    .collect(Collectors.toList());

        return !parentList.isEmpty()?ResponseUtil.get(ResponseEnum.OK,parentList)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }
    public ResponseUtil findByCategoryId(Integer categoryId) {
        Category byId = categoryMapper.findById2(categoryId);
        return ResponseUtil.get(ResponseEnum.OK, byId);
    }

    //找子类别
    @Override
    public ResponseUtil findById(Integer categoryId) {

        Category category = categoryMapper.findById2(categoryId);
        if(category.getStatus()==0){
            return ResponseUtil.get(ResponseEnum.FAIL);
        }else {
        List<Category> childList = categoryMapper.findById(categoryId);
        return  ResponseUtil.get(ResponseEnum.OK,childList);

        }
    }
    //修改类别名
    @Override
    public ResponseUtil update(Category category) {
        Category category1 = categoryMapper.findByName(category.getName());
        if (category1==null) {
            Integer update = categoryMapper.update(category);
            return update>0?ResponseUtil.get(ResponseEnum.OK):ResponseUtil.get(ResponseEnum.FAIL);
        }else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }
    //删除类别名
    @Override
    public ResponseUtil delete(Integer categoryId) {
        Category category = categoryMapper.findById2(categoryId);
        List<Product> productList =productMapper.findByCategoryId(categoryId);
        System.out.println(productList);
        if (category.getTypeId()!=0) {
            if (productList.isEmpty()) {
                categoryMapper.delete(categoryId);
                return ResponseUtil.get(ResponseEnum.OK);
            }else {
                return ResponseUtil.get(ResponseEnum.FAIL);
            }
        }else {
            List<Category> categoryList = categoryMapper.findById(categoryId);
            if (categoryList.isEmpty()) {
                categoryMapper.delete(categoryId);
                return ResponseUtil.get(ResponseEnum.OK);
            }else {
                return ResponseUtil.get(ResponseEnum.FAIL);
            }
        }
    }
    //停用启用类别
    @Override
    public ResponseUtil updateStatus(Category category) {
        category.setStatus(category.getStatus());
        Integer update = categoryMapper.update(category);
        return update>0?ResponseUtil.get(ResponseEnum.OK):ResponseUtil.get(ResponseEnum.FAIL);
    }
//    添加主类别
    @Override
    public ResponseUtil add(Category category) {
        Category category1 = categoryMapper.findByName(category.getName());
        Integer add = 0;
        if (category1==null) {
            add = categoryMapper.add(category);
        }
        return add>0?ResponseUtil.get(ResponseEnum.OK):ResponseUtil.get(ResponseEnum.FAIL);
    }
    //添加子类别
    @Override
    public ResponseUtil addSubCategory(String name,Integer typeId) {
        Integer categoryId =typeId;
        Category category = categoryMapper.findById2(categoryId);
        Category category1 = categoryMapper.findByName(name);
        Category category2 = new Category();
        if (category!=null&&category1==null) {//判断主类别是否存在
                category2.setName(name);
                category2.setTypeId(typeId);
                category2.setStatus(1);
                Integer add = categoryMapper.add(category2);
                return add>0?ResponseUtil.get(ResponseEnum.OK):ResponseUtil.get(ResponseEnum.FAIL);
        }else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }

    }

    @Override
    public ResponseUtil findByName(String name) {
        Category category = categoryMapper.findByName(name);
        return  category!=null?ResponseUtil.get(ResponseEnum.OK,category):ResponseUtil.get(ResponseEnum.FAIL);
    }


}